<?php
// +----------------------------------------------------------------------
// | Tpli [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 系统模块
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\AdminMenu as AdminMenuModel;
use app\admin\validate\Menu as MenuValidate;
use app\admin\service\AdminMenu as AdminMenuService;
class Menu extends Base
{
  public function index()
  {
    $menus = AdminMenuModel::menuList();
    $this->assign('menus',$menus);
    return $this->fetch();
  }

  public function publish()
  {
    //获取菜单id
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    $model = new AdminMenuModel();
    if($id > 0) {
      //是修改操作
      if(request()->isPost()) {
        //验证部分数据合法性
        $validate = new MenuValidate();
        if (!$validate->goCheck()) {
            $this->error('提交失败：' . $validate->getError());
        }
        $post = input('post.');
        //验证菜单是否存在
        $menu = AdminMenuModel::where('id',$id)->find();
        if(empty($menu)) {
          return $this->error('id不正确');
        }
        //如果关闭默认展开，给默认值0
        if(empty($post['is_open'])) {
            $post['is_open'] = 0;
        }
        if(false == $model->allowField(true)->save($post,['id'=>$id])) {
          return $this->error('修改失败');
        } else {
          addLog($model->id);//写入日志
          return $this->success('修改菜单信息成功','admin/menu/index');
        }
      } else {
        //重置操作
        $menu = AdminMenuModel::where('id',$id)->find();
        $menus = AdminMenuModel::select();
        $menus_all = AdminMenuService::menuTree($menus);
        $this->assign('menus',$menus_all);
        if(!empty($menu)) {
          $this->assign('menu',$menu);
          return $this->fetch();
        } else {
          return $this->error('id不正确');
        }
      }
    } else {
      //是新增操作
      if(request()->isPost()) {
        $post = input('post.');
        //验证部分数据合法性
        $validate = new MenuValidate();
        if (!$validate->goCheck()) {
            $this->error('提交失败：' . $validate->getError());
        }
        if(false == $model->allowField(true)->save($post)) {
          return $this->error('添加菜单失败');
        } else {
          addLog($model->id);//写入日志
          return $this->success('添加菜单成功','admin/menu/index');
        }
      } else {
        //重置操作
        $pid = $this->request->has('pid') ? $this->request->param('pid', null, 'intval') : null;
        if(!empty($pid)) {
          $this->assign('pid',$pid);
        }
        $menu = AdminMenuModel::select();
        $menus = AdminMenuService::menuTree($menu);
        $this->assign('menus',$menus);
        return $this->fetch();
      }
    }
  }

  public function delete()
  {
    if(request()->isAjax()) {
      $id = request()->has('id') ? request()->param('id', 0, 'intval') : 0;
      if(AdminMenuModel::where('pid',$id)->select() == null) {
        if(AdminMenuModel::where('id',$id)->delete()) {
          addLog($id);//写入日志
          return $this->success('删除成功','admin/menu/index'); 
        } else {
          return $this->error('删除失败');
        }
      } else {
          return $this->error('该菜单下还有子菜单，不能删除');
      }
    }
  }

  public function orders()
  {
    if(request()->isPost()) {
      $post = $this->request->post();
      $i = 0;
      foreach ($post['id'] as $k => $val) {
          $order = AdminMenuModel::where('id',$val)->value('orders');
          if($order != $post['orders'][$k]) {
              if(false == AdminMenuModel::where('id',$val)->update(['orders'=>$post['orders'][$k]])) {
                  return $this->error('更新失败');
              } else {
                  $i++;
              }
          }
      }
      addLog();//写入日志
      return $this->success('成功更新'.$i.'个数据','admin/menu/index');
    }
  }

}
?>